use std::pin::pin;
use crate::models::teacher::{CreateTeacher, UpdateTeacher};
use crate::state::AppState;

use actix_web::{ web,HttpResponse};
use crate::dbaccess::teacher::{delete_teacher_db, get_all_teachers_db, get_teacher_detail_db, post_new_teacher_db, update_teacher_details_db};
use crate::errors::MyError;
use crate::models::course::UpdateCourse;

pub async fn get_all_teacher(app_state: web::Data<AppState>) -> Result<HttpResponse,MyError> {
    get_all_teachers_db(&app_state.db).await
        .map(|teacher| HttpResponse::Ok().json(teacher))
}
pub async fn get_teacher_details(app_state: web::Data<AppState>,params: web::Path<i32>) -> Result<HttpResponse,MyError> {
    let teacher_id = params.into_inner();
    get_teacher_detail_db(&app_state.db, teacher_id)
        .await.map(|teacher| HttpResponse::Ok().json(teacher))
}
pub async fn post_new_teacher(new_teacher: web::Json<CreateTeacher>,app_state: web::Data<AppState>) -> Result<HttpResponse,MyError> {
    post_new_teacher_db(&app_state.db, new_teacher.into_inner())
        .await.map(|teacher| HttpResponse::Ok().json(teacher))
}

pub async fn update_teacher_details(app_state: web::Data<AppState>,params: web::Path<i32>,update_course: web::Json<UpdateTeacher>) -> Result<HttpResponse,MyError> {
    update_teacher_details_db(&app_state.db, *params, update_course.into_inner())
        .await.map(|teacher| HttpResponse::Ok().json(teacher))
}

pub async fn delete_teacher(app_state: web::Data<AppState>, id: web::Path<i32>) -> Result<HttpResponse,MyError> {
    delete_teacher_db(&app_state.db, *id).await
        .map(|teacher| HttpResponse::Ok().json(teacher))
}